package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.chuantou.yuba.R;
import com.chuantou.yuba.adapter.FriendAdapter;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.FriendDataTO;
import com.chuantou.yuba.api.to.FriendTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.ActivityHelper;
import com.chuantou.yuba.util.Constants;

/**
 * 关注者列表
 * 
 * @author Administrator
 * 
 */
public class FollowsFragment extends PaginationListFragment<FriendDataTO> {
	private FriendAdapter mAdapter;
	private boolean mNeedRefresh = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext.registerReceiver(mFollowsChangedListener, new IntentFilter(Constants.INTENT_ACITON_FRIEND_CHANGED));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View contentView = super.onCreateView(inflater, container, savedInstanceState);
		if (mAdapter == null) {
			mAdapter = new FriendAdapter(mContext);
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Object obj = parent.getItemAtPosition(position);
					if (obj != null && obj instanceof FriendTO) {
						FriendTO friend = (FriendTO) obj;
						ActivityHelper.toProfileActivity(getActivity(), friend.getUid());
					}
				}
			});
		}
		return contentView;
	}

	@Override
	protected BaseGsonRequest<FriendDataTO> getRequest(Listener<FriendDataTO> listener, ErrorListener errorListener) {
		String url = UriHelper.getFollowListUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		UserTO user = AccountManager.getInstance(mContext).getUserTO();
		params.put(UriHelper.PARAMS_PROF_UID, user != null ? String.valueOf(user.getUid()) : "0");
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		GsonRequest<FriendDataTO> request = new GsonRequest<FriendDataTO>(Method.POST, url, listener, errorListener, params, FriendDataTO.class);
		return request;
	}

	private BroadcastReceiver mFollowsChangedListener = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			mNeedRefresh = true;
		}

	};

	public void onResume() {
		super.onResume();
		if (mNeedRefresh) {
			load(1);
		}
	};

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_content);
	}

	@Override
	public void onDestroy() {
		mContext.unregisterReceiver(mFollowsChangedListener);
		super.onDestroy();
	}

	@Override
	public void onResponse(FriendDataTO response) {
		if (getActivity() != null) {
			mNeedRefresh = false;
			super.onResponse(response);
			if (response != null) {
				List<FriendTO> friends = response.getFriends();
				// 第一页
				if (response.getCurrentPage() <= 1) {
					// 保存到本地数据库缓存
					mAdapter.clear();
				}
				mAdapter.add(friends);
			} else {
				mLoadingView.setStyle(Style.EMPTY, getEmptyString());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

}
